package com.ycfmedical.bed.ui;

import com.ycfmedical.bed.R;
import com.ycfmedical.bed.service.TLSService;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.mvp.XActivity;
import tencent.tls.platform.TLSUserInfo;

public class LoginActivity extends XActivity
{
    
    private final static String TAG = "LoginActivity";
    
    @BindView(R.id.username)
    EditText username;
    
    @BindView(R.id.password)
    EditText password;
    
    @BindView(R.id.btn_login)
    Button btnLogin;

    private TLSService tlsService;
    
    final static int STR_ACCOUNT_REG_REQUEST = 20001;
    
    final static int SMS_LOGIN_REQUEST = 20002;

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_login;
    }

    @Override
    public void initData(Bundle savedInstanceState)
    {
        tlsService = TLSService.getInstance();
        initAccountLoginService();
    }


    private void initAccountLoginService()
    {
        tlsService.initAccountLoginService(LoginActivity.this, username, password, btnLogin);
        //initTLSLogin();
    }


    private void initTLSLogin()
    {
        
        runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                TLSUserInfo userInfo = tlsService.getLastUserInfo();
                if (userInfo != null)
                {
                    EditText editText = (EditText)findViewById(R.id.username);
                    editText.setText(userInfo.identifier);
                }
            }
        });
    }

    
    // 应用调用Andriod_SDK接口时，使能成功接收到回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        
        if (requestCode == STR_ACCOUNT_REG_REQUEST)
        {
            if (RESULT_OK == resultCode)
            {
                setResult(RESULT_OK, data);
                finish();
            }
        }
        else if (requestCode == SMS_LOGIN_REQUEST)
        {
            if (RESULT_OK == resultCode)
            {
                // 返回 ok 表示短信登录界面的处理是 ok 并不需要此 Activity 做什么
                setResult(RESULT_OK, data);
                finish();
            }
        }
    }
    
    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        
        setIntent(intent);
    }

    @Override
    public Object newP()
    {
        return null;
    }
}
